﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Sql;
using System.Data.SqlClient;

namespace KaoWuYou.Models
{
    public class WebUserModel
    {
        [Required]
        public string AccountName
        { set; get; }
        [Required]
        public byte Authority
        { set; get; }
        public string EmailAddress
        { set; get; }
        public DateTime LastLoginDate
        { set; get; }
        [Required]
        public string Gender
        { set; get; }
        [Required]
        public string Name
        { set; get; }
        public string NewPassword
        { set; get; }
        [Required]
        public string Password
        { set; get; }
        public string Tel
        { set; get; }
        public TestedPaperListModel TestedPapers
        { set; get; }

        public bool Login()
        {
            throw new NotImplementedException();
        }

        public bool Logoff()
        {
            throw new NotImplementedException();
        }

        public bool Register()
        {
             try 
            {
                SqlConnection conn = new SqlConnection(Systems.ConnString);
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = @"INSERT INTO Users(AccountName,EmailAddress,Password,Name,Authority,Gender,Tel,LastDate) 
                 VALUES(@AccountName,@EmailAddress,@Password,@Name,@Authority,@Gender,@Tel,@LastDate)  ";
                cmd.Parameters.AddRange(new SqlParameter[]
                    {
                        new SqlParameter("@AccoutName",AccountName),
                        new SqlParameter("@EmailAddress",EmailAddress),
                        new SqlParameter("@Password",Password),
                        new SqlParameter("@Name",Name),
                        new SqlParameter("@Authority",Authority),
                        new SqlParameter("@Gender",Gender),
                        new SqlParameter("@Tel",Tel),
                        new SqlParameter("@LastDate",DateTime.Now)
                    });
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
             }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdatePassword()
        {
            try
            {
                SqlConnection conn = new SqlConnection(Systems.ConnString);
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "UPDATE Users SET Password = @Password WHERE AccountName = @AccountName";
                cmd.Parameters.AddRange(new SqlParameter[]
                    {
                        new SqlParameter("@Password",Password),
                        new SqlParameter("@AccountName",AccountName)
                    });
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        
        public bool UpdateUserInfo()
        {
            try
            {
                SqlConnection conn = new SqlConnection(Systems.ConnString);
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "UPDATE Users SET (EmailAddress = @EmailAddress,Name = @Name,Authority = @Authority,Gender = @Gender,Tel = @Tel) WHERE AccountName = @AccountName";
                cmd.Parameters.AddRange(new SqlParameter[]
                    {
                        new SqlParameter("@EmailAddress",EmailAddress),
                        new SqlParameter("@Name",Name),
                        new SqlParameter("@Authority",Authority),
                        new SqlParameter("@Gender",Gender),
                        new SqlParameter("@Tel",Tel)
                    });
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}